package com.cheetah.start.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.EnableEnum;
import com.cheetah.start.common.enums.RoleEnum;
import com.cheetah.start.module.system.domain.SysRole;
import com.cheetah.start.module.system.mapper.SysRoleMapper;
import com.cheetah.start.module.system.service.ISysRoleAdminService;
import com.cheetah.start.module.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色服务
 *
 * @author Feel
 * @date 2021/09/05
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {


    @Autowired
    private ISysRoleAdminService roleAdminService;

    /**
     * 获取页面
     *
     * @param pageQuery 页面查询
     * @param role      角色
     * @return {@link ResultTable}
     */
    @Override
    public ResultTable getPage(PageQuery pageQuery, SysRole role) {
        Page<SysRole> res = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        if (StrUtil.isNotBlank(role.getRoleName())) {
            wrapper.like("role_name", role.getRoleName());
        }
        if (StrUtil.isNotBlank(role.getRoleCode())) {
            wrapper.like("role_code", role.getRoleCode());
        }
        page(res, wrapper);
        return ResultTable.pageTable(res.getTotal(), res.getRecords());
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        SysRole role = getById(roleId);
        return role != null && RoleEnum.SUPER_ADMIN.getCode().equals(role.getRoleCode());
    }

    @Override
    public boolean isSuperAdminDisable(SysRole role) {
        boolean b = RoleEnum.SUPER_ADMIN.getCode().equals(role.getRoleCode());
        return b && EnableEnum.DISABLE.getCode().equals(role.getEnable());
    }

    @Override
    public List<SysRole> getAllRoleWithCheck(Integer adminId) {
        List<SysRole> list = list();
        List<SysRole> check = roleAdminService.getByAdminId(adminId);
        if (CollUtil.isNotEmpty(list)) {
            for (SysRole sysRole : list) {
                if (CollUtil.isNotEmpty(check)) {
                    for (SysRole role : check) {
                        if (role.getRoleId().equals(sysRole.getRoleId())) {
                            sysRole.setChecked(true);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<SysRole> listEnable() {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("enable", EnableEnum.ENABLE.getCode());
        return list(wrapper);
    }
}
